<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Submanager extends PNC_Controller {

	public function __construct() {
		parent::__construct();
		$this->check_access();
		$this->load->model(array('mod_gm', 'mod_staff', 'mod_record', 'mod_option', 'mod_leave_request'));
	}

	/*
	 * Index
	 * @access public
	 * @return void
	 */

	public function index() {
		redirect('submanager/home/');
	}

	/*
	 * Show Record
	 * @access public
	 * @return void
	 */
	/* View home page of user */

	public function home() {
		if ($this->check_session()) {
			$data['title'] = "Home";
			$session_id = $this->session->userdata('id_user');
			$data['datas'] = $this->mod_staff->selectdata($session_id);
			$data['total'] = $this->mod_staff->total($session_id);
			$this->view_main($data);
		} else {
			redirect('main/');
		}
	}

	/* Show all pending and approve of record */

	public function report() {
		if ($this->check_session()) {
			$this->session->set_userdata("cur_page", $this->uri->segment(3));
			$this->session->set_userdata("pagination", $this->uri->segment(4));
			$data['title'] = 'Record';
			$config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 0);
			$this->session->set_userdata('num_pending', $config['total_rows']);
			$num_per_page = 10;
			/* in case user delete query string */
			if ($this->session->userdata('per_page')) {
				$this->session->unset_userdata('per_page');
			}
			/* get limit per page from query string */
			if ($this->input->get('limit', TRUE)) {
				$this->session->set_userdata('per_page', $this->input->get('limit', TRUE));
				$num_per_page = $this->session->userdata('per_page');
			}
			$config['total_rows'] = ''; // initialize total records
			$config['base_url'] = site_url('submanager/report/pending'); // initialize base url
			if ($this->uri->segment(3) === 'pending' OR !$this->uri->segment(3)) {
				$config['base_url'] = site_url('submanager/report/pending');
				$config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 0);
			} else if ($this->uri->segment(3) === 'approved') {
				$config['base_url'] = site_url('submanager/report/approved');
				$config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 1);
			}
			$config['per_page'] = $num_per_page;
			$config['uri_segment'] = 4;
			$choice = $config["total_rows"] / $config["per_page"];
			$config["num_links"] = round($choice);
			$config['first_link'] = 'First';
			$config['next_link'] = 'Next';
			$config['prev_link'] = 'Pre';
			$config['last_link'] = 'Last';

			$this->pagination->initialize($config); //function to show all pages
			$page = ($this->uri->segment($config['uri_segment']) && $this->uri->segment($config['uri_segment']) > 0) ? $this->uri->segment($config['uri_segment']) : 0;
			$data['leave_record'] = ''; // initalize record seleted
			if ($this->uri->segment(3) === 'pending' OR !$this->uri->segment(3)) {
				/* get leave recrod as pending */
				$data['leave_record'] = $this->mod_record->approved_padding($config['per_page'], $page, 'pending', $this->session->userdata('staff_id'));
			} else if ($this->uri->segment(3) === 'approved') {
				/* get leave record as approved */
				$data['leave_record'] = $this->mod_record->approved_padding($config['per_page'], $page, 'approved', $this->session->userdata('staff_id'));
			}
			$data['pagination'] = $this->pagination->create_links();
			$this->view_main($data);
		} else {
			redirect('main/');
		}
	}

	/* When user complete old or wrong password */

	public function change_pass() {
		$data['title'] = 'Change Password';
		if ($this->input->post('btn_save_change')) {
			$this->change_password();
			redirect('submanager/get_profile');
		}
		$this->view_main($data);
	}

	/* Make Leaving Request */

	public function leaving() {
		if ($this->check_session()) {
			$data['title'] = "Do Request";
			$data['validator'] = $this->mod_option->get_manage_mail('manager', $this->session->userdata('staff_id'));
			$data['super'] = $this->mod_leave_request->super_type_leave();
			$data['type'] = $this->mod_leave_request->type_leave();
			$data['sub_type'] = $this->mod_leave_request->sub_type_leave();
			$data['sub_type_per'] = $this->mod_leave_request->sub_type_leave_per();
			if ($this->input->post('btn_do_request')) {
				$config = array(
					array(
						'field' => 'duration',
						'label' => 'Time Duration',
						'rules' => 'trim|required|xss_clean'
					),
					array(
						'field' => 'reason',
						'label' => 'Reason',
						'rules' => 'trim|required|xss_clean'
					),
					array(
						'field' => 'super_type',
						'label' => 'Type of Leave',
						'rules' => 'trim|required|greater_than[0]'
					),
					array(
						'field' => 'validators',
						'label' => 'Manager Mail',
						'rules' => 'trim|required|valid_email'
					),
					array(
						'field' => 's_date',
						'label' => ' Start Date',
						'rules' => 'trim|required|callback_compareDates'
					),
					array(
						'field' => 'e_date',
						'label' => 'End Date',
						'rules' => 'trim|required|callback_compareDates'
					)
				);
				$this->form_validation->set_rules($config);
				$this->form_validation->set_message('greater_than[0]', 'The field must be select.');
				if ($this->form_validation->run() === FALSE) {
					$this->session->set_userdata('validate_fail', show_message('Please check your completed form!', 'error'));
					$this->view_main($data);
				} else {
					$staff = $this->session->userdata('staff_id');
					$validator = $this->input->post('validators');
					$super_type = $this->input->post('super_type');
					$sub_type = $this->input->post('sub');
					$sub_sub = $this->input->post('sub_sub');
					$desc = $this->input->post('desc_up');
					$work_on = $this->input->post('working_on');
					$catch_on = $this->input->post('catchup_on');
					$reason = $this->input->post('reason');
					$start_date = $this->input->post('s_date');
					$end_date = $this->input->post('e_date');
					$hour = $this->input->post('duration');

					$this->session->set_userdata('reciever_mail', $validator);
					$this->session->set_userdata('reason_leaving', $reason);

					$day = ($hour / 8);

					if ($start_date <= $end_date) {
						$success_request = '';
						if ($super_type == 1) {
							$success_request = PNC_Model::add_record($staff, $validator, $super_type, $sub_type, $sub_sub, '', $reason, $start_date, $end_date, $hour, $day, '', '');
							if ($success_request > 0) {
								$this->send_mail_request('submanager', 'submanager', 'info', 'leaving');
							} else {
								$this->session->set_userdata('send_request_erro', show_message('Sending email for do request was failed!', 'error'));
								$this->view_main($data);
							}
						} else if ($super_type == 2) {
							$success_request = PNC_Model::add_record($staff, $validator, $super_type, '', '', $desc, $reason, $start_date, $end_date, $hour, $day, $work_on, $catch_on);
							if ($success_request > 0) {
								$this->send_mail_request('submanager', 'submanager', 'info', 'leaving');
							} else {
								$this->session->set_userdata('sending_erro', show_message('Can not do request leaving!', 'error'));
								$this->view_main($data);
							}
						}
					} else {
						$this->session->set_userdata('sent_erro', show_message('Start date must be smaller than end date!', 'error'));
						$this->view_main($data);
					}
				}
			} else {
				$this->view_main($data);
			}
		}
	}

	/* Change personal numbers leaving request */

	public function info() {
		if ($this->check_session()) {
			$this->session->set_userdata("cur_page", $this->uri->segment(3));
			$this->session->set_userdata("pagination", $this->uri->segment(4));
			$data['title'] = 'Personal leaving Request';
			$config['total_rows'] = PNC_Model::personal_leaving($this->session->userdata('staff_id'), 'pending');
			$this->session->set_userdata('num_own_pending', $config['total_rows']);
			$num_per_page = 5;
			/* in case user delete query string */
			if ($this->session->userdata('per_page')) {
				$this->session->unset_userdata('per_page');
			}
			/* get limit per page from query string */
			if ($this->input->get('limit', TRUE)) {
				$this->session->set_userdata('per_page', $this->input->get('limit', TRUE));
				$num_per_page = $this->session->userdata('per_page');
			}
			$config['total_rows'] = ''; // initialize total records
			$config['base_url'] = site_url('submanager/info/own_pending'); // initialize base url
			if ($this->uri->segment(3) === 'own_pending' OR !$this->uri->segment(3)) {
				$config['base_url'] = site_url('submanager/info/own_pending');
				$config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 0);
			} else if ($this->uri->segment(3) === 'own_approved') {
				$config['base_url'] = site_url('submanager/info/own_approved');
				$config['total_rows'] = $this->mod_gm->count_report_padding($this->session->userdata('staff_id'), 1);
			}
			$config['per_page'] = $num_per_page;
			$config['uri_segment'] = 4;
			$choice = $config["total_rows"] / $config["per_page"];
			$config["num_links"] = round($choice);
			$config['first_link'] = 'First';
			$config['next_link'] = 'Next';
			$config['prev_link'] = 'Pre';
			$config['last_link'] = 'Last';

			$this->pagination->initialize($config); //function to show all pages
			$page = ($this->uri->segment($config['uri_segment']) && $this->uri->segment($config['uri_segment']) > 0) ? $this->uri->segment($config['uri_segment']) : 0;
			$data['leave_record'] = ''; // initalize record seleted
			if ($this->uri->segment(3) === 'own_pending' OR !$this->uri->segment(3)) {
				/* get leave recrod as pending */
				$data['leave_record'] = $this->mod_record->approved_padding($config['per_page'], $page, 'own_pending', $this->session->userdata('staff_id'));
			} else if ($this->uri->segment(3) === 'own_approved') {
				/* get leave record as approved */
				$data['leave_record'] = $this->mod_record->approved_padding($config['per_page'], $page, 'own_approved', $this->session->userdata('staff_id'));
			}
			$data['pagination'] = $this->pagination->create_links();
			$this->view_main($data);
		} else {
			redirect('main/');
		}
	}

	/* Change status of record leaving request when onclick */

	public function approval() {
		if ($this->check_session()) {
			$this->approval_record($this->uri->segment(5), $this->uri->segment(4), $this->uri->segment(3), 'submanager', 'info', 'report', 'own_pending', 'type_pending', 'disapprove');
		} else {
			redirect('main/');
		}
	}

	/* View Profile */

	public function get_profile() {
		if ($this->check_session()) {
			$data['title'] = 'Profile';
			$data['get_profile'] = PNC_Model::get_profile($this->session->userdata('staff_id'));

			$this->view_main($data);
		} else {
			redirect('main/');
		}
	}

	/* Change new password */

	public function change_password() {
		if ($this->check_session()) {
			if ($this->input->post('btn_save_change')) {
				$old_pass = $this->input->post('old_pass');
				$new_pass = $this->input->post('new_pass');
				$con_pass = $this->input->post('con_pass');
				$old_pass = md5($old_pass);
				$pass = $this->session->userdata('old_pass');

				if ($old_pass === '' OR $new_pass === '' OR $con_pass === '') {
					$this->session->set_userdata('input_blank', show_message('Please input all fields!', 'error'));
					redirect('submanager/change_pass');
				} else if (strlen($new_pass) < 5 OR strlen($new_pass) > 15) {
					$this->session->set_userdata('min_length', show_message('Password must be longer than 5 and shorter than 15 characters!', 'error'));
					redirect('record/change_pass');
				} else if ($old_pass != $pass) {
					$this->session->set_userdata('opass_err', show_message('Your old password is not match!', 'error'));
					redirect('submanager/change_pass');
				} else if ($new_pass != $con_pass) {
					$this->session->set_userdata('npass_err', show_message('New password and confirm password is not match!', 'error'));
					redirect('submanager/change_pass');
				} else {
					$new_pass = md5($new_pass);
					PNC_Model::change_password($old_pass, $new_pass, $con_pass, $this->session->userdata('staff_id'));
					$this->session->set_userdata('change_success', show_message('Changed password successfully!', 'success'));
					redirect('submanager/get_profile');
				}
			}
		} else {
			redirect('main/');
		}
	}

	/* Update before having approve from validator */

	public function update_before_approve() {
		if ($this->check_session()) {
			$this->session->set_userdata('old_lea_id', $this->uri->segment(3));
			$this->session->set_userdata('old_desc_id', $this->uri->segment(5));
			$this->session->set_userdata('rcd_id', $this->uri->segment(4));
			if ($this->uri->segment(3) == 1) {
				$data['edit_annual'] = PNC_Model::show_detail($this->uri->segment(4), 'annual');
			} else if ($this->uri->segment(3) == 2) {
				$data['edit_catchup'] = PNC_Model::show_detail($this->uri->segment(4), 'catchup');
			}
			$data['title'] = 'Edit Leaving Request';
			$data['hour'] = array('' => '-- Select --', '4' => '4 Hours', '8' => '8 Hours', '12' => '12 Hours', '16' => '16 Hours');
			$data['rcd_status'] = array('' => '-- Select --', '0' => 'Disapprove', '1' => 'Approve');
			$data['leave_type'] = $this->mod_option->get_leaving_type();
			$data['sub_type'] = $this->mod_option->get_sub_leaving();
			$data['validator'] = $this->mod_option->get_validator();
			$data['ssl_type'] = $this->mod_option->get_ssl_type();
			if ($this->input->post('btn_update')) {
				$old_lea_id = $this->input->post('old_lea_id');
				$old_desc_id = $this->input->post('old_desc_id');
				$record_id = $this->input->post('record_id');
				/* Old post from form update */
				$old_sub_type = $this->input->post('old_sub_type');
				$old_ss_type = $this->input->post('old_ss_type');
				$old_reason = $this->input->post('old_reason');
				$old_start_date = $this->input->post('old_s_date');
				$old_end_date = $this->input->post('old_e_date');
				$old_desc_up = $this->input->post('old_desc_up');
				$old_working_on = $this->input->post('old_working_on');
				$old_catchup_on = $this->input->post('old_catchup_on');
				$old_hour = $this->input->post('old_duration');
				/* Check and Compare value */
				$supper_type = !$this->input->post('super_type') ? $old_lea_id : $this->input->post('super_type');
				$sub_type = !$this->input->post('sub_type') ? $old_sub_type : $this->input->post('sub_type');
				$ss_type = !$this->input->post('ss_type') ? $old_ss_type : $this->input->post('ss_type');
				$reason = !$this->input->post('txt_reason') ? $old_reason : $this->input->post('txt_reason');
				$start_date = !$this->input->post('s_date') ? $old_start_date : $this->input->post('s_date');
				$end_date = !$this->input->post('e_date') ? $old_end_date : $this->input->post('e_date');
				$desc_up = !$this->input->post('desc_up') ? $old_desc_up : $this->input->post('desc_up');
				$working_on = !$this->input->post('working_on') ? $old_working_on : $this->input->post('working_on');
				$catchup_on = !$this->input->post('catchup_on') ? $old_catchup_on : $this->input->post('catchup_on');
				$hour = !$this->input->post('duration') ? $old_hour : $this->input->post('duration');
				$days = ($hour / 8);
//				echo 'Sub type '.$sup_type; die();
				$result = '';
				/* If update with change leave type */
				if ($supper_type != $old_lea_id) {
					if ($supper_type == 1) {
						PNC_Model::delete_datas('desc_id', $old_desc_id, '', 'pnc_cup_des');
						$result = PNC_Model::update_annual_before($record_id, $supper_type, $sub_type, $ss_type, $reason, $start_date, $end_date, $hour, $days);
					} else if ($supper_type == 2) {
						$result = PNC_Model::update_catchup_before($record_id, $supper_type, $reason, $start_date, $end_date, $hour, $days, $desc_up, $working_on, $catchup_on);
					}
				} else {
					if ($supper_type == 1) {
						$result = PNC_Model::update_annual_before($record_id, $supper_type, $sub_type, $ss_type, $reason, $start_date, $end_date, $hour, $days);
					} else if ($supper_type == 2) {
						$result = PNC_Model::update_catchup($record_id, $old_desc_id, $supper_type, $reason, $start_date, $end_date, $hour, $days, $desc_up, $working_on, $catchup_on);
					}
				}
				if ($result > 0) {
					$this->session->set_userdata('edit_success', show_message('Edited successfully!', 'success'));
					if ($this->session->userdata('per_page')) {
						redirect('submanager/info/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
					}
					redirect('submanager/info/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
				} else {
					$this->session->set_userdata('edit_erro', show_message('Editing was fail!', 'error'));
					redirect('submanager/info/update_before_approve/' . $this->session->userdata('pagination'));
				}
			} else {
				$this->view_main($data);
			}
		} else {
			redirect('main/');
		}
	}

	/* Delete leaving request before validator approve */

	public function delete_before_approve() {
		if ($this->check_session()) {
			$this->delete_before_approving('submanager', 'report');
		} else {
			redirect('main/');
		}
	}

	public function logout_submanager() {
		$this->session->unset_userdata('submanager');
		$this->session->set_userdata('logout', show_message('You have been logout!', 'notice'));
		redirect('main/');
	}

}

/* End of file submanager.php */
/* Location: ./application/controllers/submanager.php */